<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户号" prop="subMerchantNo">
        <el-input v-model="queryParams.subMerchantNo" placeholder="请输入商户号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="账号" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecs:agencyOperations:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="agencyOperationsList" row-key="ypwId"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="100" align="center" prop="userId" />
      <el-table-column label="账号" align="center" prop="userName" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="提交时间" align="center" prop="saveTime" />
      <el-table-column label="状态 " align="center" prop="status">
        <template slot-scope="{ row }">
          <span v-if="row.status === '1'">待提交</span>
          <span v-else-if="row.status === '2'">审核中</span>
          <span v-else-if="row.status === '3'">审核驳回</span>
          <span v-else-if="row.status === '4'">协议待签署</span>
          <span v-else-if="row.status === '5'">申请完成</span>
          <span v-else-if="row.status === '6'">业务开通中</span>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="fail" width="180" />
      <el-table-column label="入网费率" v-if="user.userId === 1" align="center" prop="feeRate" width="110" />
      <el-table-column label="商户号" prop="subMerchantNo" align="center" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === '1' || scope.row.status === '3'" size="mini" type="text"
            icon="el-icon-position" @click="handOperateVisible(true, false, scope.row)"
            v-hasPermi="['ecs:agencyOperations:edit']">修改</el-button>
          <el-button size="mini" type="text"
            v-if="scope.row.status === '5' || scope.row.status === '6' || scope.row.status === '2' || scope.row.status === '4'"
            @click="handleDetail(true, scope.row)">详情</el-button>
          <el-link v-if="scope.row.status === '4'" :href="scope.row.agreementSignUrl" style="margin-left: 5px"
            target="_blank" type="primary" :underline="false">签署协议</el-link>
        </template>
      </el-table-column>
    </el-table>
    <detail :opens="openDetail" :type="1" :id="form.id" v-if="openDetail" :title="'详情'"
      @handOperateVisible="handleDetail">
    </detail>
    <operate :opens="operateVisible" :type="1" :id="form.id" v-if="operateVisible" :title="'代运营资质补充'"
      @handOperateVisible="handOperateVisible"></operate>
  </div>
</template>

<script>
import { listAgencyOperations, delAgencyOperations } from "@/api/ecs/agencyOperations";
import detail from '@/views/ecs/incoming/yeePayWithholding/modules/detail.vue'
import operate from '@/views/ecs/incoming/yeePayWithholding/modules/operate.vue'
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'

export default {
  name: "AgencyOperations",
  components: {
    operate,
    detail
  },
  dicts: ['sys_normal_disable', 'yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      statusOptions: [
        { value: "1", label: "待提交" },
        { value: "2", label: "审核中" },
        { value: "3", label: "审核驳回" },
        { value: "4", label: "协议待签署" },
        { value: "5", label: "申请完成" },
        { value: "6", label: "业务开通中" },
      ],
      // 代运营表格数据
      agencyOperationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subMerchantNo: '',
        userName: "",
        nickName: "",
        status: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "代理账号不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '代理账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: "代理昵称不能为空", trigger: "blur" }
        ],
        phoneNumber: [
          { required: true, message: "代理联系电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的联系电话",
            trigger: "blur"
          }
        ],
        password: [
          // { required: true, message: "密码不能为空", trigger: "blur" },
          // { min: 5, max: 20, message: '密码长度必须介于 5 和 20 之间', trigger: 'blur' },
          { pattern: /^[^<>"'|\\]+$/, messagge: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
      },
      detailData: {},
      operateVisible: false,
      openDetail: false,
      user: {}
    };
  },
  created() {
    this.loading = true;
    getUserProfile().then(response => {
      this.loading = false;
      this.user = response.data;
      this.getList();
    }).catch(() => {
      this.loading = false;
    })

  },
  methods: {
    /** 查询代运营列表 */
    getList() {
      this.loading = true;
      listAgencyOperations(this.queryParams).then(response => {
        this.agencyOperationsList = this.handleTree(response.data, "id", "agentId");
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        nickName: null,
        feeRate: null,
        remark: null,
        password: null,
        phoneNumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.operateVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.operateVisible = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除代运营编号为"' + ids + '"的数据项？').then(function () {
        return delAgencyOperations(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/agencyOperations/export', {
        ...this.queryParams
      }, `agencyOperations_${new Date().getTime()}.xlsx`)
    },
    handOperateVisible(val, val1, val2) {
      if (val) {
        this.form.id = val2.ypwId
      }
      this.operateVisible = val
      if (val1) {
        this.getList()
      }
    },

    handleDetail(val, val1) {
      if (val) {
        this.form.id = val1.ypwId
      } else {
        this.form.id = null
      }
      this.openDetail = val
    },
  }
};
</script>
<style scoped lang="scss">
.form {
  padding-left: 10px;
  padding-right: 10px;
  padding-bottom: 80px;
}

.footer-operation {
  bottom: 0;
  width: 100%;
  height: 80px;
  display: flex;
  z-index: 10000;
  padding: 0 20px;
  text-align: center;
  position: absolute;
  align-items: center;
  background-color: #ffffff;
  border-top: 1px solid #EBEEF5;

  .el-button {
    width: 100%;
  }
}
</style>
